<?php
$access_level = 1;
$page_title = 'eTAT: Create Survey';
$script ='survey';
require_once '../controller.php';

$instructor_id = $_SESSION['logged_in'];
$s = new Survey();
if( isset($_POST['create'])) {

	$date = date('Y-m-d');
	$new_survey = new Survey();
      
    $data = array(
            'instructor_id'=>$instructor_id,
            'survey_name' => addslashes($_POST['name']),
            'survey_title' => addslashes($_POST['title']),
            'survey_welcome_message' => addslashes($_POST['welcome_message']),
            'survey_confirmation_message' => addslashes($_POST['confirmation_message']),
            'email_subject'=>addslashes($_POST['title']),
            'email_message'=>addslashes($_POST['welcome_message']),
            'date_created'=>$date,
            'last_modified'=>$date
            
    );
    if(isset($_REQUEST['sid'])){
        $sid =$_REQUEST['sid'];
        if(!isset($_POST['name'])){
        	$survey = $s->getSurvey($sid);
        	$data['survey_name']= $survey['name'];
        }
        if($new_survey->updateSurvey($sid,$data)){
        header("Location:question.php?sid=$sid");
        }else {
        	echo'<font color="red">Cannot Update Survey Information</font>';
        }
    }else{
        if($new_survey->createSurvey($data)){
    	header('Location:question.php');
        }else{
        	echo'<font color="red">Survey name already exists. Please choose a different name</font>';
        }
    }
     
}

if(isset($_POST['copy'])){
	$sid = $_POST['copy'];
	header("location:copy.php?sid=$sid");
}

if(isset($_POST['cancel'])){
	header("location:interface.php");
}
/******************************************************************************************************/

require_once '../header.php';

if(isset($_REQUEST['sid'])){

		$sid = $_REQUEST['sid'];
	
	$s = new Survey($sid);
	$survey = $s->getSurvey($sid);
		
}

echo '<div>';

echo 'Create New Survey: Please Fill in the Information.  <br><br>';

?>

<table>
<form name="create_survey" method="POST" onsubmit="return validate()">
<tr><th>
<font color="red" size="5em">*</font>Survey-Name:</th><td><input name="name" class="required" type="text"
	value="<?php if(isset($_REQUEST['sid'])){ echo $survey['name'];}?>" <?php if(isset($_REQUEST['sid'])){
		     if(!strpos($survey['name'],"copy"))echo 'disabled="disabled"';} ?>"> </td></tr>
<tr><td>&nbsp;</td></tr>
<tr><th>
<font color="red" size="5em">*</font>Survey-Title: </th><td><input type="text" 	class="required" name="title"
	value="<?php if(isset($_REQUEST['sid'])){ echo $survey['title'];}?>"></td></tr>
<tr><td>&nbsp;</td></tr>
<tr><th>
Welcome Message: </th><td><textarea  rows="2" cols="30" name="welcome_message">
	<?php if(isset($_REQUEST['sid'])){ echo $survey['welcome_message'];}?></textarea></td></tr>
<tr><td>&nbsp;</td></tr>
<tr><th>
Confirmation Message: </th><td><textarea rows="2" cols="30" name="confirmation_message">
	<?php if(isset($_REQUEST['sid'])){ echo $survey['confirmation_message'];}?></textarea></td>
</tr>
<tr><td>&nbsp;</td></tr>
<tr>
<td><input type="submit" name="cancel" value="Cancel"</td>
<td>&nbsp;</td><td>&nbsp;</td><td>&nbsp;</td><td>&nbsp;</td>
<td><input type="submit" name="create" value="Save & Continue">
</td></tr>
</form>
</table>
<br></br>


<div class='clear'></div>
<?php 
require_once '../footer.php';
?>
